package com.wujunshen.algorithm.leetcode.dynamic.programming;

/**
 * @author frank woo(吴峻申) <br>
 * email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2022/8/11 16:37<br>
 */
public class 区域和检索数组不可变 {
    public static void main(String[] args){
        int[] nums=new int[]{-2, 0, 3, -5, 2, -1};
        int left=0;
        int right=2;

        NumArray obj = new NumArray(nums);
        int result = obj.sumRange(left,right);
        System.out.println("result is:"+result);
    }
}

class NumArray {
    int[] sums;
    public NumArray(int[] nums) {
        int n = nums.length;
        sums = new int[n + 1];
        for (int i = 0; i < n; i++) {
            sums[i + 1] = sums[i] + nums[i];
        }
    }

    public int sumRange(int left, int right) {
        return sums[right + 1] - sums[left];
    }
}
